#! /bin/bash

clone_and_deploy()
{
    echo "clone_and_deploy"
    
    if [ -z "$repo_url" ]; then
        repo_url=$1
    fi
    
    if [ -z "$third_part_name" ]; then
        third_part_name=$2
    fi

    if [ -z "$third_part_repo_tag" ]; then
        local third_part_repo_tag=$3
    fi

    if [ -z "$script_dir" ]; then
        script_dir=$4
    fi
    
    local cache_dir=$script_dir/cache
    
    if [ -z "$repo_url" ] ;then
        echo "repo_url is empty"
        exit -1
    fi 

    if [ -z "$third_part_name" ] ;then
        echo "third_part_name is empty"
        exit -1
    fi 

    if [ -z "$third_part_repo_tag" ] ;then
        echo "third_part_repo_tag is empty"
        exit -1
    fi 

    if [ ! -d "$cache_dir" ] ;then
        mkdir -p $cache_dir

        if [ ! -d "$cache_dir" ] ;then
            echo "create $cache_dir failed"
            exit -1
        fi
    fi

    export local_repo_dir=$cache_dir/$third_part_name

    if [ -n "$re_clone" ]; then
        rm -rf $local_repo_dir
    fi

    
    if [ ! -d "$local_repo_dir" ] ;then
        echo "clone $repo_url to $local_repo_dir"
        git clone -b $third_part_repo_tag $repo_url $local_repo_dir
        
        if [ -n "$commit_id" ]; then
            cd $local_repo_dir

            git checkout $commit_id

            cd $script_dir
        fi
    fi

    cd $local_repo_dir

    if [ -n "$commit_id" ]; then
        cd $local_repo_dir

        git checkout $commit_id

        cd $script_dir
    else
        git pull 
    fi

    cd $script_dir

}